package com.hnust.controller;

import com.hnust.domain.Teacher;
import com.hnust.domain.record.*;
import com.hnust.service.TeacherService;
import com.hnust.service.TokenService;
import com.hnust.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *创建人：张汝往
 *创建时间：2020/10/6
 *描述：老师列表模块
 */
@CrossOrigin("*")
@RestController
@RequestMapping("/api/teacher")
public class TeacherController {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private TeacherService teacherService;

    /**
     * 点击老师列表时显示学院内老师的信息
     * @param token token信息
     * @param collegeId 学院ID
     * @return
     */
    @GetMapping("/get")
    public Result<List<SimpleTeacherInfo>> getSimpleTeacherInfo(
            @RequestParam("token")String token,
            @RequestParam("collegeId")String collegeId
    ){
      if(tokenService.verifyToken(token,"院管理员")){
          List<SimpleTeacherInfo> simpleTeacherInfos=teacherService.searchSimpleTeacherInfo(collegeId,null);
          return new Result<List<SimpleTeacherInfo>>(1,"获取数据成功",simpleTeacherInfos);
      }else{
          return new Result<>(0,"没有操作权限,请登录",null);
      }
    }

    /**
     * 根据学院id查找老师
     * @param map 前端返回数据（token，collegeId，searchTeacherInfo）
     * @return
     */
    @PostMapping("/query")
    public Result<List<SimpleTeacherInfo>> querySimpleTeacherInfo(
            @RequestBody Map<String, Object> map)
    {
        String token=map.get("token").toString();
        String collegeId=map.get("collegeId").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("searchTeacherInfo"));
        SearchTeacherInfo searchTeacherInfo=(SearchTeacherInfo) JSONObject.toBean(jsonObject, SearchTeacherInfo.class);

        if(tokenService.verifyToken(token,"院管理员")){
            List<SimpleTeacherInfo> simpleTeacherInfos=teacherService.searchSimpleTeacherInfo(collegeId,searchTeacherInfo);
            return new Result<List<SimpleTeacherInfo>>(1,"获取数据成功",simpleTeacherInfos);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 查看老师的详细信息
     * @param token token 信息
     * @param teacherId 老师ID
     * @return
     */
    @GetMapping("/get/teacherInfo")
    public Result<TeacherInfo> getTeacherInfo(
            @RequestParam("token")String token,
            @RequestParam("teacherId")String teacherId

    ){
        if(tokenService.verifyToken(token,"院管理员")){
            TeacherInfo teacherInfo=teacherService.getTeacherInfo(teacherId);
            return new Result<TeacherInfo>(1,"获取数据成功",teacherInfo);
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    @PostMapping("/addOneTeacher")
    public Result<Boolean> addOneTeacher(@RequestBody Map<String, Object> map){
        String token=map.get("token").toString();
        // System.out.println(student);
        if(tokenService.verifyToken(token,"院管理员")){
            try {
                String collegeId=map.get("collegeId").toString();
                JSONObject jsonObject = JSONObject.fromObject(map.get("teacher"));
//                System.out.println(jsonObject);
                AddOneTeacher teacher = (AddOneTeacher) JSONObject.toBean(jsonObject, AddOneTeacher.class);
                if (teacherService.addOneTeacher(teacher, collegeId)) {
                    return new Result<Boolean>(1, "添加成功", null);
                } else {
                    return new Result<Boolean>(0, "添加失败", null);
                }
            }catch (Exception e){
                System.out.println(e);
                return new Result<Boolean>(0, "添加失败", null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 删除老师信息
     * @param token token
     * @param teacherId 老师信息
     * @return
     */
    @GetMapping("/delete")
    public Result<Boolean> deleteTeacher(
            @RequestParam("token")String token,
            @RequestParam("teacherId")String teacherId
    ){
        System.out.println(teacherId);
        if(tokenService.verifyToken(token,"院管理员")){
            if(teacherService.deleteTeacher(teacherId)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"删除失败，请确保该老师没有关联数据！",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 批量删除老师信息
     * @param map 前端返回数据（token，teacherIds）
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> massDeleteTeacher(@RequestBody Map<String,Object> map){
        String token=map.get("token").toString();
        JSONArray teacherIds= JSONArray.fromObject(map.get("teacherIds"));
        if(tokenService.verifyToken(token,"院管理员")){
            try {
                teacherService.massDeleteTeacher(teacherIds);
                return new Result<>(1,"操作成功",null);
            }catch (Exception e){
                return new Result<>(0,"删除失败，请确保该老师没有关联数据！",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

    /**
     * 修改老师信息
     * @param map 返回前端数据（token，teacherInfo）
     * @return
     */
    @PostMapping("/modify")
    public Result<Boolean> modifyTeacher(@RequestBody Map<String,Object> map){
        String token=map.get("token").toString();
        JSONObject jsonObject=JSONObject.fromObject(map.get("teacherInfo"));
        TeacherInfo teacherInfo=(TeacherInfo) JSONObject.toBean(jsonObject, TeacherInfo.class);
        //System.out.println(teacherInfo);
        if(tokenService.verifyToken(token,"院管理员")){
            if(teacherService.modifyTeacher(teacherInfo)){
                return new Result<>(1,"操作成功",null);
            }else{
                return new Result<>(0,"操作失败",null);
            }
        }else{
            return new Result<>(0,"没有操作权限,请登录",null);
        }
    }

}
